package top.fengleifeng.question;

/**
 * @author : fengleifeng
 * @Package top.fengleifeng.question
 * @Description:
 * @date Date : 2020年12月24日 7:42 下午
 */
public class Num135分发糖果 {

    class Solution {

        public int candy(int[] ratings) {
            if (ratings.length <= 1) {
                return 1;
            }
            int index = 1;
            int[] dp = new int[ratings.length];
            dp[0] = 1;
            int count = 0;
            for (int i = 1; i < ratings.length; i++) {
                if (ratings[i] > ratings[i - 1]) {
                    dp[i] = dp[i - 1] + 1;
                } else if (ratings[i] == ratings[i - 1]) {
                    dp[i] = 1;
                } else {
                    dp[i] = 1;
                    if (dp[i - 1] <= dp[i]) {
                        changeBefore(i, ratings, dp);
                    }
                }
            }
            for (int one : dp
            ) {
                count += one;
            }
            return count;
        }

        private void changeBefore(int index, int[] ratings, int[] dp) {
            for (int i = index; i > 0; i--) {
                if (ratings[i - 1] > ratings[i] && dp[i-1]<=dp[i]) {
                    dp[i - 1] = dp[i - 1] + 1;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] ints = {3,2,1,2};
//        int[] ints = {1, 0, 2};
//        int[] ints = {1, 2, 2};
//        int[] ints = {1, 3, 2, 2, 1};// 1 2 1 1 1
        int candy = new Num135分发糖果().new Solution().candy(ints);
        System.out.println(candy);
    }
}
